package com.yitiao.designmodel.create.prototype;

import java.util.HashMap;
import java.util.Map;

public class YitiaoBatis{
    //缓存Map
    private Map<String,Yitiao> yitiaoCache = new HashMap<>();

    //从缓存拿对象
    public Yitiao getYitiao(String name) throws CloneNotSupportedException {
        //判断缓存中是否存在
        if (yitiaoCache.containsKey(name)){
            Yitiao yitiao = yitiaoCache.get(name);
            System.out.println("从缓存查到数据："+yitiao);
            //修改返回
            //return yitiao;
            return yitiao.clone();
        }else {
            //模拟从数据库查数据
            Yitiao yitiao = new Yitiao();
            yitiao.setName(name);
            yitiao.setId(1);
            yitiao.setWechat("公众号：一条coding");
            System.out.println("从数据库查到数据:"+yitiao);
            //放入缓存
            yitiaoCache.put(name,yitiao);
            //修改返回
            //return yitiao;
            return yitiao.clone();
        }
    }
}
